<?php
// 说明:2015-07-06  by abc777
// 1.先获取账号的供应商列表，获取的产品必须在这些供应商中，已发现BUG：现供销关系断开还能购买还能转分销。
// 2.获取直销中的产品权限 pft_product_sale_list。
// 3.获取转分销中的产品   pft_p_apply_evolute 
// 4.

include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/class/abc/PFTCoreAPI.class.php';
include_once '/var/www/html/new/d/common/area.inc.php';


if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$memberSID = $sid = $_SESSION['sid'];// 会员ID
$salePros  = PFTCoreAPI::salePros(array('fields'=>array('l.area')));

if(isset($_REQUEST['debug'])) print_r($salePros);

// 景区模糊查询
$lids = array();
if(isset($_REQUEST['title']) && (!isset($_REQUEST['search_type']) || $_REQUEST['search_type']==0)){
	$ltitle = $_REQUEST['title'];
	$sql = "select id from uu_land where title like '%$ltitle%'";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $lids[] = $row['id'];
}
$lids_count = count($lids);// 模糊查询出来的景区ID



// 分页依据
$city        = isset($_REQUEST['city']) ? $_REQUEST['city']:'';
$provice     = isset($_REQUEST['provice']) ? $_REQUEST['provice']:'';
$pageSize    = isset($_REQUEST['pageSize']) ? $_REQUEST['pageSize']+0:15;
$currentPage = isset($_REQUEST['currentPage']) ? $_REQUEST['currentPage']+0:1; 
if($pageSize<1) $pageSize = 15;
if($currentPage<1) $currentPage = 1;

// 按供应商搜索
$suppliers_t = PFTCoreAPI::Suppliers($memberSID);

// 按供应商搜索
if($_REQUEST['supplier']){
	$key = array_search($_REQUEST['supplier'], $suppliers_t);
	if($key===false) exit(json_encode(array('lists' => array(), 'total' => 0, 'code'=>200)));
	$suppliers_t = array($key=>$_REQUEST['supplier']);
}


// 按景区划分
foreach($salePros as $row){
	
	if($lids_count && !in_array($row['lid'], $lids)) continue;
	if(!isset($suppliers_t[$row['sapply_did']])) continue;
	if($row['area']!=''){
		$area_a = explode('|', $row['area']);// 拆分地区
		if($city && !in_array($city, $area_a)) continue;
		if($provice && !in_array($provice, $area_a)) continue;
	}
	
	$row['city']    = $areas[$area_a[1]];
	$row['provice'] = $areas[$area_a[0]];
	$lands[$row['lid']][] = $row;
}

if(isset($_REQUEST['debug'])) print_r($lands);

// 直销是否开启转分销
$sql = "select id,pid,sid from pft_p_apply_evolute where fid={$_SESSION['sid']} and lvl=1 and active=1 and status=0";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()) $active_t[$row['pid']] = $row['sid']; 

if(isset($_REQUEST['debug'])) print_r($active_t);

$totalRow  = count($lands);
$totalPage = ceil($totalRow/$pageSize);

$count  = 0;
$point  = 0;
$today  = date('Y-m-d');
$pStart = ($currentPage-1) * $pageSize;
$sales  = array();
if(count($lands)){ 
	foreach($lands as $tickets){
		

		if(($point++)<$pStart) continue;
		if(($count++)>=$pageSize) break;
		foreach($tickets as $ticket){
			
			$pStorage = PFTCoreAPI::pStorage($s, $_SESSION['saccount'], $ticket['pid'], $ticket['sapply_did'], $today);

			if(!isset($sales[$ticket['lid']])){// 景区信息	
				$land['lid']   = $ticket['lid'];
				$land['city']  = $ticket['city'];
				$land['title'] = $ticket['ltitle'];
				$land['province']  = $ticket['provice'];
				$land['memberSID'] = $memberSID;
				$sales[$ticket['lid']] = $land;
			}
			
			$ticket['js']  = $pStorage['js']['p'];
			$ticket['ls']  = $pStorage['ls']['p'];
			$ticket['aid'] = $ticket['sapply_did'];
			$ticket['active'] = (isset($active_t[$ticket['pid']]) && $active_t[$ticket['pid']]==$ticket['sapply_did'])? 1:$ticket['active'];
			$ticket['dname']  = $suppliers_t[$ticket['sapply_did']];
			$ticket['conf_price'] = 1;// 前端是否显示配置分销价格按钮条件 1 配置
			if($pStorage['js']['p']==-1) $ticket['conf_price'] = 0;
			
			if($_SESSION['sid']!=1433){
				if(isset($_SESSION['alliance']) || $ticket['lvl']>3) $ticket['conf_price'] = 0; 
			}
					
			$sales[$ticket['lid']]['ticket'][$ticket['tid']]['pid']     = $ticket['pid'];
			$sales[$ticket['lid']]['ticket'][$ticket['tid']]['title']   = $ticket['ttitle'];
			$sales[$ticket['lid']]['ticket'][$ticket['tid']]['lists'][] = $ticket;
		}
	}
}



// 没有转分销产品
if(count($sales)==0) exit(json_encode(array('lists' => array(), 'total' => 0, 'code'=>200)));


$response = array( 'lists' => $sales, 'totalPage' => $totalPage, 'code' => 200, 'currentPage' => $currentPage, 'pageSize'=>$pageSize);
echo json_encode($response);
// print_r($response);




function formatEvoData($params, $memberSID){
	$params['fid']      = $memberSID;
	$params['sid']      = $params['apply_did'];
	$params['lvl']      = 0;
	$params['aids']     = $params['apply_did'];
	$params['status']   = 0;
	$params['active']   = 0;
	$params['sourceid'] = $params['apply_did'];
	return $params;
}
